package cn.xval.ft.app;

import cn.xval.fc.base.FcBase;
import cn.xval.fc.core.FcCore;
import cn.xval.fc.design.FcDesign;
import cn.xval.ft.grpc.CollectInfo;
import cn.xval.ft.grpc.MarketDataGrpc;
import cn.xval.ft.grpc.StartCollectRequest;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@SpringBootApplication(scanBasePackageClasses = {
        Main.class,
        FcBase.class,
        FcDesign.class, // 代码生成，不要代码生成时，可以注释掉
        FcCore.class
})
@EnableJpaAuditing
public class Main implements ApplicationRunner  {
//    static {
//        System.loadLibrary("thostmduserapi_se");
//        System.loadLibrary("thosttraderapi_se");
//        System.loadLibrary("wrap");
//    }


    public static void main(String[] args) {
        testGrpc();
//        connect();
//        log.info("合约:{} {} 最新价:{} 买:{}/{} 卖:{}/{}", dm, updatetime, lastprice, buyprice, buyvol, sellprice, sellvol);
        SpringApplication.run(Main.class, args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {

    }

    private static void testGrpc() {
        ManagedChannel ch = ManagedChannelBuilder
                .forAddress("127.0.0.1", 10240)
                .usePlaintext()
                .build();
        var stub = MarketDataGrpc.newBlockingStub(ch);
        var collect = StartCollectRequest.newBuilder();

//                .addCollections(CollectInfo.newBuilder()
//                        .setBrokerId("9999")
//                        .setFrontResolveType(0)
//                        .setHost("tcp://180.168.146.187:10131")
//                        .setUserId("218679")
//                        .setPassword("")
//                );
//                .build();
        stub.start(collect.build());
    }

//    private static File tempDir;
//
//    static {
//        try {
//            tempDir = Files.createTempDirectory("ctp_libs").toFile();
////            System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + tempDir.getAbsolutePath());
//            loadLibrary("thostmduserapi_se.dll");
//            loadLibrary("thosttraderapi_se.dll");
//            loadLibrary("ctp4java_wrap.dll");
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }

//    public static void loadLibrary(String libraryName) throws IOException {
//        // 获取本地库文件的输入流
//        ClassPathResource res = new ClassPathResource("lib/" + libraryName);
//        try (var inputStream = res.getInputStream()) {
////        InputStream inputStream = NativeLibraryLoader.class.getResourceAsStream("/lib/" + libraryName);
////        new ResourceStr
//            // 创建临时目录
////            File tempDir = Files.createTempDirectory("native_libs").toFile();
//
//            // 将本地库文件复制到临时目录
//            File libFile = new File(tempDir, libraryName);
//            try (OutputStream outputStream = new FileOutputStream(libFile)) {
//                byte[] buffer = new byte[1024];
//                int length;
//                while ((length = inputStream.read(buffer)) != -1) {
//                    outputStream.write(buffer, 0, length);
//                }
//            }
//
//            // 将临时目录添加到java.library.path中
////            System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + tempDir.getAbsolutePath());
//            System.loadLibrary(libraryName);
//        }
//    }
}